//
//  ClassListViewController.m
//  Second
//
//  Created by xalo on 16/7/16.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "ClassListViewController.h"

@interface ClassListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong)UICollectionView *collertionView;
// cell上的图片
@property (nonatomic,strong)NSMutableArray *datasource;
//最新列表的数据的起始位置
@property (nonatomic,assign)NSInteger newStart;
@property (nonatomic,assign)NSInteger pageid;
@property (nonatomic,assign)BOOL isLoad;
@end

@implementation ClassListViewController

- (UICollectionView *)collertionView {
    
    if (!_collertionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        _collertionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:layout];
        _collertionView.dataSource = self;
        _collertionView.delegate = self;
        layout.itemSize = CGSizeMake(kWidth/2-15, kWidth/2+50);
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
        
        
    }
    return _collertionView;
}

- (NSMutableArray *)datasource {
    
    if (!_datasource) {
        _datasource = [NSMutableArray array];
    }
    return _datasource;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.collertionView registerNib:[UINib nibWithNibName:@"ClassListCell" bundle:nil] forCellWithReuseIdentifier:@"ClassListCell"];
    
    self.collertionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collertionView];
    
    //数据请求
    self.pageid = 1;
    [self loadClassListWithPageid:self.pageid];
    //添加下拉刷新
    self.collertionView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        
        self.isLoad = YES;
        [self loadClassListWithPageid:self.pageid];

    }];
    
    //添加上拉加载
    self.collertionView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.pageid++;
        [self loadClassListWithPageid:self.pageid];
        
    }];

}
//加载数据
- (void)loadClassListWithPageid:(NSInteger)pageid{
    [RequestManager fecthDataWithUrl:@"http://youzixy.com/app/sale/goods" parameter:@{@"type":self.typeID,@"page":@(pageid)} mehtod:@"POST" compelet:^(NSData *data) {
        NSDictionary *bigdic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",bigdic);
        
        if (self.isLoad && bigdic.count!=0) {
            [self.datasource removeAllObjects];
            self.isLoad = NO;
        }
        
        for (NSDictionary *dic in bigdic[@"res"]) {
            
            ClassListModel *model = [[ClassListModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.datasource addObject:model];
        }
        
    } updateUI:^{
        NSLog(@"刷新UI");
        //结束上拉加载
        [self.collertionView.mj_footer endRefreshing];
        //结束下拉刷新
        [self.collertionView.mj_header endRefreshing];
        //刷新tableView
        
        
        [self.collertionView reloadData];
    }];

}




#pragma mark -----------collectionViewDegelate-----------

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.datasource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ClassListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ClassListCell" forIndexPath:indexPath];
    [cell setCellWithModelll:self.datasource[indexPath.row]];
    
    cell.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];
    cell.layer.masksToBounds = YES;
    cell.layer.cornerRadius = 10;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    XMSecondController *second = [[XMSecondController alloc]init];
    [self.navigationController pushViewController:second animated:YES];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
